package chances.epg.data.taglib;

import java.util.List;

import javax.servlet.jsp.JspException;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;

import chances.epg.commons.log.EpgLogFactory;
import chances.epg.entity.biz.BizPosition;
import chances.epg.entity.biz.BizPositionItem;
import chances.epg.navigator.context.AccessContextUtils;


public class BizPositionTag extends QueryTag {
	private static final Logger logger = EpgLogFactory.getSystemLogger();
	
	/**
	 * serialVersionUID.
	 */
	private static final long serialVersionUID = -509156866346177997L;
	private String code;//推荐位编码
	
	@Override
	public int doEndTag() throws JspException {
		try{
			BizPosition position = this.getBizPositionService().getBizPositionByCode(code);
			if(position != null){
				List<BizPositionItem> items = position.getItems();
				for (BizPositionItem bizPositionItem : items) {
					if(StringUtils.isNotBlank(bizPositionItem.getObjValue())){
						String url = navigateUrlFactory.generateURL(AccessContextUtils.getAccessContext(this.pageContext), bizPositionItem);
						bizPositionItem.setUrl(url);
					}
				}
				this.setResult(position);
			}
		}catch(Throwable ex){
			logger.error("BizPositionTag error",ex);
		}
		return super.doEndTag();
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}
}
